from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
import json
import sqlite3

class ClickLabel(QLabel):
    # 自定义信号, 注意信号必须为类属性
    button_clicked_signal = pyqtSignal()

    def __init__(self, parent=None):
        super(ClickLabel, self).__init__(parent)

    def mouseReleaseEvent(self, QMouseEvent):
        self.button_clicked_signal.emit()
        
    # 可在外部与槽函数连接
    def connect_customized_slot(self, func):
        self.button_clicked_signal.connect(func)

class IndicatorLine(QWidget):
    def __init__(self, parent=None) -> None:
        super().__init__(parent)
        self.line_left = QLineEdit(self)
        self.line_middle = QLineEdit(self)
        self.line_right = QLineEdit(self)
        self.line_left.move(0,0)
        self.line_middle.move(195,0)
        self.line_right.move(390,0)
        self.line_left.textEdited[str].connect(lambda: self.on_change())

    def getText(self):
        return [self.line_left.text(),self.line_middle.text(),self.line_right.text()]

    def setText(self,values:list):
        self.line_left.setText(values[0])
        self.line_middle.setText(values[1])
        self.line_right.setText(values[2])

    def setSize(self,width,height):
        self.line_left.setFixedSize(width,height)
        self.line_middle.setFixedSize(width,height)
        self.line_right.setFixedSize(width,height)

    def setMinimumSize(self, width,height) -> None:
        self.line_left.setMinimumSize(width,height)
        self.line_middle.setMinimumSize(width,height)
        self.line_right.setMinimumSize(width,height)

    def get_pointer(self,pointer):
        self.pointer = pointer

    def on_change(self):
        tmp = Indicator()
        try:
            classname = self.pointer.class_name_line.text()
            classcode = self.pointer.class_code_line.text()
            rlt = tmp.search(classname=classname,classcode=classcode)
            if rlt:
                self.line_left.setText(self.line_left.text()+rlt[self.line_left.text()])
        except:
            pass

class Indicator():
    def __init__(self):
        try:
            self.conn = sqlite3.connect('class_manager.db')
        except:
            print("connect error")

    def search(self,classname,classcode):
        sql = "select * from class where classname='"+classname+"' and classcode='"+classcode+"'"
        cursor = self.conn.cursor()
        result = cursor.execute(sql).fetchone()
        if not result:
            return None
        else:
            data =  json.loads(result[-1])
            dic = {}
            for i in range(len(data)):
                tmp = data[i].split()
                dic[tmp[0]] = tmp[1]
            return dic

class HboxBarWidget(QWidget):
    def __init__(self,w=40,h=35,widget_list=[],is_all=0):
        super().__init__()
        self.widget_list = widget_list
        spacer = QSpacerItem(w, h, QSizePolicy.Minimum, QSizePolicy.Expanding)
        self.layout = QHBoxLayout()
        self.setLayout(self.layout)
        if is_all==0 or is_all==1:
            self.layout.addItem(spacer)
        for i in range(len(self.widget_list)):
            self.layout.addWidget(self.widget_list[i])
        if is_all==0 or is_all==2:
            self.layout.addItem(spacer)


class RegisterBar(QWidget):
    def __init__(self):
        super().__init__()
        self.setFixedHeight(45)
        self.setMinimumWidth(360)
        self.login_label = QLabel("登录")
        login_font = QFont('Microsoft YaHei',22)
        login_font.setPixelSize(22)
        self.login_label.setFont(login_font)
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(0,0,0,0)
        self.setLayout(self.layout)
        self.widget = QWidget()
        self.widget.setFixedWidth(80)
        self.user_label = QLabel("新用户",self.widget)
        self.user_label.move(0,17)
        self.register_label = ClickLabel(self.widget)
        self.register_label.move(40,17)
        self.register_label.setText("注册")
        self.layout.addWidget(self.login_label)
        self.layout.addWidget(self.widget)
        self.register_label.setStyleSheet(
            'QLabel{color:#92c9fb;}'
            'QLabel:hover{color:#92c9fb;}'
        )
        self.user_label.setStyleSheet(
            'QLabel{color:#7f7f7f;}'
        )
        self.login_label.setStyleSheet(
            'QLabel{color:#7f7f7f;}'
        )


class AttachmentWidget(QWidget):
    def __init__(self, parent = None,path="static/file.jpg",title="附件清单") -> None:
        """
        主要用于page2中添加和删除相关附件
        """
        super().__init__(parent=parent)
        self.path = path
        self.title = title
        self.setMinimumSize(290,350)
        self.layout = QVBoxLayout()
        self.setLayout(self.layout)
        self.layout.setContentsMargins(0,0,0,0)
        #对头部图片和标题进行设计
        self.top_bar_widget = QWidget()
        self.top_bar_widget.setMinimumSize(40,35)
        self.pic = QPixmap(path)
        self.pic = self.pic.scaled(35, 30)
        self.top_label = QLabel(self.top_bar_widget)
        self.top_label.setPixmap(self.pic)
        self.tittle_lab = QLabel(self.title,self.top_bar_widget)
        self.tittle_lab.move(40,5)
        f = QFont("PingFang SC")
        f.setPixelSize(18)
        self.tittle_lab.setFont(f)
        #对中间的表格进行设计
        self.table = QTableWidget()
        #对底部的按钮进行设计
        self.bottom_widget = QWidget()
        self.bottom_widget.layout = QHBoxLayout()
        self.bottom_widget.layout.setContentsMargins(0,20,0,0)
        self.bottom_widget.setLayout(self.bottom_widget.layout)
        self.add_btn = QPushButton("新增")
        self.del_btn = QPushButton("删除")
        self.bottom_widget.layout.addWidget(self.add_btn)
        self.bottom_widget.layout.addWidget(self.del_btn)
        self.layout.addWidget(self.top_bar_widget)
        self.layout.addWidget(self.table)
        self.layout.addWidget(self.bottom_widget)


if __name__=="__main__":
    app = QApplication(sys.argv)
    window = AttachmentWidget()
    window.show()
    sys.exit(app.exec_())